<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\models\Region;

/* @var $this yii\web\View */
/* @var $searchModel common\models\RegionSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '地区';
//$this->params['breadcrumbs'][] = $this->title;

$this->params['breadcrumbs'][] = ['label' => '地区', 'url' => ['index','RegionSearch'=>['parent_id'=>0]]];

$query=Yii::$app->request->queryParams;
if(isset($query['RegionSearch']['parent_id']) && $query['RegionSearch']['parent_id']>0){
    $node = Region::findOne(['id'=>$query['RegionSearch']['parent_id']]);
    if($node && !empty($node->route)){
        $routeIds = array_merge($node->route,[$node->id]);
        for($i=1;$i<count($routeIds);$i++){
            $node = Region::findOne(['id'=>$routeIds[$i]]);
            if($node){
                $this->params['breadcrumbs'][] = ['label' => $node->name, 'url' => ['index','RegionSearch'=>['parent_id'=>$node->id]]];
            }
        }
    }
}

?>

<div class="region-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('新建', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?=
    GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            [
                'attribute' => 'name',
                'content' => function($data, $row) {
                    return $data->childCount > 0 ? Html::a($data->name, ['index', 'RegionSearch' => ['parent_id' => $data->id]]) : $data->name;
                },
            ],
            //'parent_id',
            [
                'class' => 'yii\grid\ActionColumn',
                'buttons' => [
                    'delete' => function ($url, $model, $key) {
                        $del = Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, ['data-confirm' => '您确定要删除此项吗？', 'data-method' => 'post', 'data-pjax' => 0]);
                        return $model->childCount == 0 ? $del : '';
                    },
                ],
            ],
        ],
    ]);
    ?>

</div>
